//
//  UPMainViewController.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/17.
//

#import "UPTabBarViewController.h"
#import "UPFirstActivityViewController.h"
#import "UPMineInfomationViewController.h"
#import <Masonry/Masonry.h>

@interface UPTabBarViewController ()

@property (nonatomic, strong) UIViewController *currentViewController;
@property (nonatomic, copy) NSArray<UIViewController *> *viewControllers;

@end

@implementation UPTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCustomTabBar];
    [self setupViewControllers];
}

- (void)setupCustomTabBar {
    _customTabBar = [[UPCustomTabBarView alloc] init];
    _customTabBar.backgroundColor = [UIColor clearColor];
    _customTabBar.delegate = self;
    
    _customTabBar.itemTitles = @[
        LocalizableForKey(@"Home"),
        LocalizableForKey(@"Mine")
    ];
    
    _customTabBar.normalColor = [UIColor colorWithHexString:@"#848484"];
    _customTabBar.selectedColor = [UIColor colorWithHexString:@"#FFFFFF"];
    
    [self.view addSubview:_customTabBar];
    [_customTabBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.equalTo(@(mainTabBarHeight));
        make.leading.trailing.equalTo(self.view);
    }];
    
    _customTabBar.barHeight = mainTabBarHeight;
}

- (void)setCustomTabBarHidden:(BOOL)hidden {
    _customTabBar.hidden = hidden;
}

- (void)setupViewControllers {
    _viewControllers = @[
        [[UPNavigationController alloc] initWithRootViewController:[[UPFirstActivityViewController alloc] init]],
        [[UPNavigationController alloc] initWithRootViewController:[[UPMineInfomationViewController alloc] init]]
    ];
    
    // 初始显示第一个视图控制器
    [self changeViewControllerToIndex:0];
}

- (void)changeViewControllerToIndex:(NSInteger)index {
    // 移除当前显示的视图控制器
    [_currentViewController willMoveToParentViewController:nil];
    [_currentViewController.view removeFromSuperview];
    [_currentViewController removeFromParentViewController];
    
    // 添加新的视图控制器
    UIViewController *newVC = _viewControllers[index];
    
//    if ([newVC isKindOfClass:[UPNavigationController class]]) {
//        UPNavigationController *nav = (UPNavigationController *)newVC;
//        if ([nav.viewControllers.firstObject isKindOfClass:[UPEmohViewController class]]) {
//            // [(UPEmohViewController *)nav.viewControllers.firstObject updateData];
//        } else if ([nav.viewControllers.firstObject isKindOfClass:[UPOrderViewController class]]) {
//            [(UPOrderViewController *)nav.viewControllers.firstObject updateDataWithTab:@""];
//        } else if ([nav.viewControllers.firstObject isKindOfClass:[UPUserViewController class]]) {
//            [(UPUserViewController *)nav.viewControllers.firstObject updateData];
//        }
//    }
    
    [self addChildViewController:newVC];
    [self.view insertSubview:newVC.view belowSubview:_customTabBar];
    newVC.view.frame = self.view.bounds;
    [newVC didMoveToParentViewController:self];
    _customTabBar.selectedIndex = index;
    
    if (index == 0) {
        _customTabBar.bgImageView.image = [UIImage imageNamed:@"tabbar_item1"];
    } else if (index == 1) {
        _customTabBar.bgImageView.image = [UIImage imageNamed:@"tabbar_item2"];
    }
    _currentViewController = newVC;
}

#pragma mark - UPCustomTabBarViewDelegate

- (void)tabBar:(UPCustomTabBarView *)tabBar didSelectItemAtIndex:(NSInteger)index {
    [self changeViewControllerToIndex:index];
}

@end
